package photobox.manage

import grails.converters.JSON
import photobox.BaseController
import photobox.photo.PhotoFramePrice
import photobox.photo.PhotoFrame
import photobox.photo.PhotoSize
import photobox.utils.ParamUtils
import photobox.utils.ResultUtil

class MngPhotoFramePriceController extends BaseController {
    def  photoFramePriceService

    def index() {}

    def list() {
        def searchParams = ParamUtils.parseFilter(params.filters)
        def pageParams = ParamUtils.parsePage(params)
        def sortParams = ParamUtils.parseSort(params,
                ["id", "photoSize", "price", "isDeal"], [sort: 'id', order: "desc"])


        def photoFramePriceRs = photoFramePriceService.list(searchParams, pageParams, sortParams)
        def dataList = []
        def totalRow = 0
        def curPage = pageParams.page
        def pageSize = pageParams.pageSize
        if (curPage <= 0) curPage = 1
        if (pageSize <= 0) pageSize = 5
        if (pageSize >=100) pageSize = 100
        if (totalRow == null) totalRow = 0
        int totalPage = (totalRow +  pageSize -1) / pageSize
        if(photoFramePriceRs.rs){
            totalRow = photoFramePriceRs.total
            photoFramePriceRs.rs.each {
                dataList.push(
                        id:it.id,price:it.price,photoSize:it.photoSize?.name,photoFrame:it.photoFrame?.name,isDeal:it.isDeal
                )
            }
        }
        def result = [:]
        result.put("datas",dataList)
        result.put("records",totalRow)
        result.put("page",curPage)
        result.put("total",totalPage)
        render result as JSON
    }

    def create() {
        def photoFrameList = PhotoFrame.findAll()
        def photoSizeList = PhotoSize.findAll()
        [photoFrameList:photoFrameList,photoSizeList:photoSizeList]
    }

    def save() {
        photoFramePriceService.createPhotoFramePrice(params)
        render([error: 0, message: "创建新相框价格完成"] as JSON)
    }

    def edit() {
        def photoFrameList = PhotoFrame.findAll()
        def photoSizeList = PhotoSize.findAll()
        def photoFramePrice = PhotoFramePrice.get(params.id)
        [photoFramePrice: photoFramePrice,photoFrameList:photoFrameList,photoSizeList:photoSizeList]
    }

    def update() {
        photoFramePriceService.updatePhotoFramePrice(params)
        render([error: 0, message: "更新相框价格信息完成"] as JSON)
    }

    def delete() {
        photoFramePriceService.deletePhotoFramePrice(params)
        render([error:0, message: "删除相框价格完成"] as JSON)
    }
}
